<?php

namespace app\admin\controller;

use think\Db;
use think\Validate;

class Templete extends Common
{
    public function occupation()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.type');

        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $order = input('order') ? input('order') : 'asc';
        if ($order == 1) {
            $order = 'desc';
        } else if ($order == 2) {
            $order = 'asc';
        }

        $data = db('user_occupation')->where($where)->order('des ' . $order . ',id ' . $order)->paginate(_getCachePages(), false, ['query' => request()->param()]);
        $page = $data->render();

        $this->assign('data', $data);
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function addoccupation()
    {
        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');

            if (!$id) { //添加数据
                unset($post['url']);
                $res = db('user_occupation')->insert($post);

                if ($res) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else { //修改数据

                $res = db('user_occupation')->where('id', $id)->update($post);

                if ($res) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = db('user_occupation')->where('id', $id)->find();

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }

    public function deloccupation()
    {
        if (request()->isAjax()) {
            $pid = input('id');
            if ($pid) {
                $res = db('user_occupation')->delete($pid);
                if ($res) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }


    public function pingjia()
    {
        $where = [];
        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $types = input('types');
        if ($types) {
            $where['types'] = $types;
        }

        $data = db('evaluate_default')->where($where)->order('id desc')->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        $this->assign('types', $types);

        return $this->fetch();
    }

    public function addpingjia()
    {
        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');

            if (!$id) {  //添加数据

                $res = db('evaluate_default')->insert($post);
                if ($res) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {//修改数据
                $res = db('evaluate_default')->where('id', $id)->update($post);
                if ($res) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }

        } else {
            if ($id) {
                $name = db('evaluate_default')->where('id', $id)->find();

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }

            $data = $this->fetch();
        }

        return $data;
    }

    public function delpingjia()
    {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = db('evaluate_default')->delete($id);
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }


    public function words()
    {
        $kefu = Db::name('config_system')->where(['category'=>'kefu','title'=>'id'])->value('content');
        $where = ['user_id' => $kefu];
        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $channel = input('channel');
        if ($channel) {
            $where['channel'] = $channel;
        }

        $data = Db::name('privatechat_default')->where($where)->order('id desc')->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        $this->assign('channel', $channel);

        return $this->fetch();
    }

    public function addWords()
    {
        $kefu = Db::name('config_system')->where(['category'=>'kefu','title'=>'id'])->value('content');
        $id = input("id");
        cache("word&0&$kefu", NULL);
        if (request()->isAjax()) {
            $post = input('post.');
            $post['user_id'] = $kefu;
            $post['shenhe_state'] = 1;
            if (!$id) {  //添加数据

                $res = Db::name('privatechat_default')->insert($post);
                if ($res) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {//修改数据
                $res = Db::name('privatechat_default')->where('id', $id)->update($post);
                if ($res) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }

        } else {
            if ($id) {
                $name = Db::name('privatechat_default')->where('id', $id)->find();

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }

            $data = $this->fetch();
        }

        return $data;
    }

    public function delWords()
    {
        $kefu = Db::name('config_system')->where(['category'=>'kefu','title'=>'id'])->value('content');
        cache("word&0&$kefu", NULL);
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = Db::name('privatechat_default')->delete($id);
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }


    public function zhaohu()
    {
        $where = [];
        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $types = input('types');
        if ($types) {
            $where['types'] = $types;
        }

        $data = db('privatechat_zhaohu')->where($where)->order('id desc')->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        $this->assign('types', $types);

        return $this->fetch();
    }

    public function addzhaohu()
    {
        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');;

            if (!$id) {  //添加数据

                $res = db('privatechat_zhaohu')->insert($post);
                if ($res) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {//修改数据
                $res = db('privatechat_zhaohu')->where('id', $id)->update($post);
                if ($res) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
            cache('systemzhaohuall', null);
        } else {
            if ($id) {
                $name = db('privatechat_zhaohu')->where('id', $id)->find();

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }

            $data = $this->fetch();
        }

        return $data;
    }

    public function delzhaohu()
    {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = db('privatechat_zhaohu')->delete($id);
                if ($db) {
                    $data = ['ret' => '删除成功'];
                    cache('systemzhaohuall', null);
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }


    public function slogan()
    {
        $where = [];
        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $types = input('types');
        if ($types) {
            $where['types'] = $types;
        }

        $data = Db::name('tpl_slogan')->where($where)->order('id desc')->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        $this->assign('types', $types);

        return $this->fetch();
    }

    public function addslogan()
    {
        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');

            if (!$id) {

                $res = Db::name('tpl_slogan')->insert($post);
                if ($res) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $res = Db::name('tpl_slogan')->where('id', $id)->update($post);
                if ($res) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('tpl_slogan')->where('id', $id)->find();

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }

            $data = $this->fetch();
        }

        return $data;
    }

    public function delslogan()
    {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = Db::name('tpl_slogan')->delete($id);
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }


    public function block()
    {
        $where = ['id' => ['gt', 0]];

        $data = Db::name('speaker_block')->where($where)->order('id desc')->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function addBlock()
    {
        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');;

            if (!$id) {

                $res = Db::name('speaker_block')->insert($post);
                if ($res) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $res = Db::name('speaker_block')->where('id', $id)->update($post);
                if ($res) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('speaker_block')->find($id);

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }

            $data = $this->fetch();
        }

        return $data;
    }

    public function delBlock()
    {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = Db::name('speaker_block')->delete($id);
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }


    public function background()
    {
        $where = ['id' => ['gt', 0]];

        $data = Db::name('speaker_background')->where($where)->order('id desc')->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function addBackground()
    {
        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');;

            if (!$id) {

                $res = Db::name('speaker_background')->insert($post);
                if ($res) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $res = Db::name('speaker_background')->where('id', $id)->update($post);
                if ($res) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('speaker_background')->find($id);

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }

            $data = $this->fetch();
        }

        return $data;
    }

    public function delBackground()
    {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = Db::name('speaker_background')->delete($id);
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }

    public function labels()
    {
        $where = [];

        $top = input('top') ?: 1;
        $where['parent_id'] = $top == 1 ? 0 : ['neq', 0];

        $search_name = input('param.name');
        $search_search = input('param.search');

        if ($search_name && $search_search) {
            if ($search_search == 'parent_id') {
                $where[$search_search] = $search_name;

                $top = 2;
            } else {
                $where[$search_search] = ['like', '%' . $search_name . '%'];
            }
        }

        $data = Db::name('labels')->where($where)->order('sort desc, id asc')->paginate(_getCachePages(), false, ['query' => request()->param()]);
        $page = $data->render();

        $this->assign('data', $data);
        $this->assign('page', $page);

        $this->assign('top', $top);

        return $this->fetch();
    }

    public function addLabels()
    {
        $category = Db::name('labels')->where(['parent_id' => 0, 'is_hidden' => 0])->order('sort desc,id asc')->select();
        $this->assign('category', $category);

        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');

            if (!$id) {
                $res = Db::name('labels')->insert($post);

                $data = $res ? ['ret' => '添加成功'] : ['err' => '添加失败'];
            } else {
                $res = Db::name('labels')->where('id', $id)->update($post);

                $data = $res ? ['ret' => '修改成功'] : ['err' => '修改失败'];
            }
        } else {
            if ($id) {
                $name = Db::name('labels')->find($id);

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }

    public function delLabels()
    {
        if (request()->isAjax()) {
            $pid = input('id');
            if ($pid) {
                $res = Db::name('labels')->delete($pid);
                if ($res) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }




    public function labelcreate()
    {
        $shenhe = input('shenhe/d');
        
        if ($shenhe === null) {
            $shenhe = 0;
        }
        $where['t1.state'] = $shenhe;
        $this->assign('shenhe', $shenhe);

        $order = input('order') ? input('order') : 'desc';
        if ($order == 1) {
            $order = 'desc';
        } else if ($order == 2) {
            $order = 'asc';
        }

        $data = Db::name('label_create_apply t1')
                    ->field('t1.*')
                    ->join('user t2', 't2.id = t1.user_id')
                    ->where($where)
                    ->order('t1.id '.$order)
                    ->paginate(_getCachePages(), false, ['query' => request()->param()])
                    ->each(function($item){
                        $item['parent'] = Db::name('labels')->find($item['parent_id']);
                        return $item;
                    });

        $page = $data->render();

        $this->assign('data', $data);
        $this->assign('page', $page);

        return $this->fetch();
    }
    public function labelcreateshenhe()
    {
        $id = input('id');

        if (request()->isAjax()) {

            $post = input('post.');

            $label_create_apply = db('label_create_apply')->where('id', $id)->find();
            if ($id && $label_create_apply) {
                $db = db('label_create_apply')->where('id', $id)->update(['state' => $post['shenhe']]);
                if($post['shenhe'] == 1){
                    Db::name('labels')->insert(['name'=>$label_create_apply['title'], 'parent_id'=>$label_create_apply['parent_id'], 'icon'=>$label_create_apply['icon'], 'description'=>$label_create_apply['description'], ]);
                }
                if ($db) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            } else {
                $data = ["err" => '参数缺失'];
            }
        }
        return $data;
    }
    public function labelcreateshenheAll()
    {
        if (request()->isAjax()) {
            $post = input('post.');
            $type = input('type');
            if (input('?post.delAll')) {
                $post = $post['delAll'];
                $db = Db::name('label_create_apply');
                foreach ($post as $k => $v) {
                    $label_create_apply = $db->where('id', $v)->find();
                    if ($type == 'del') {
                        $db->delete($v);
                    } else {
                        $db->where('id', $v)->setField('state', $type);
                        if($type == 1){
                            Db::name('labels')->insert(['name'=>$label_create_apply['title'], 'parent_id'=>$label_create_apply['parent_id'], 'icon'=>$label_create_apply['icon'], 'description'=>$label_create_apply['description'], ]);
                        }
                    }
                }
                if ($db) {
                    $data = ['ret' => '操作成功'];
                } else {
                    $data = ['err' => '操作失败'];
                }
            } else {
                $data = ['err' => '操作失败，未选中数据'];
            }
        } else {
            $data = ['err' => '提交类型错误'];
        }
        return $data;
    }
    public function labelcreateDel()
    {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $label_create_apply = Db::name('label_create_apply')->where(['id' => $id])->find();
                $db = Db::name('label_create_apply')->delete($id);
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }




    public function comment()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.type');

        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $order = input('order') ? input('order') : 'asc';
        if ($order == 1) {
            $order = 'desc';
        } else if ($order == 2) {
            $order = 'asc';
        }

        $data = db('dycomment_template')->where($where)->order('time ' . $order . ',id ' . $order)->paginate(_getCachePages(), false, ['query' => request()->param()]);
        $page = $data->render();

        $this->assign('data', $data);
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function addcomment()
    {
        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');

            if (!$id) { //添加数据
                $timenow = time();
                $contents = explode(';', $post['content']);
                $contents = array_filter(array_unique($contents));
                $inserts = [];
                foreach ($contents as $key => $value) {
                    if ($value) {
                        $inserts[] = [
                            'content'=>$value,
                            'sex'=>$post['sex'],
                            'time'=>$timenow
                        ];
                    }
                }
                $res = false;
                if (sizeof($inserts)) {
                    $res = db('dycomment_template')->insertAll($inserts);
                }

                if ($res) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else { //修改数据

                $res = db('dycomment_template')->where('id', $id)->update($post);

                if ($res) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = db('dycomment_template')->where('id', $id)->find();

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }

    public function delcomment()
    {
        if (request()->isAjax()) {
            $pid = input('id');
            if ($pid) {
                $res = db('dycomment_template')->delete($pid);
                if ($res) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }

    public function delcommentAll($delAll = [])
    {
        $res = db('dycomment_template')->delete($delAll);
        if ($res) {
            $data = ['ret' => '删除成功'];
        } else {
            $data = ['err' => '删除失败'];
        }
        return $data;
    }




    // 动态分类
    public function userRole()
    {
        $where = [];
        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $category = Db::name('user_entertainment')->where($where)->field('*, parent_id tid')->order('sort desc,id asc')->select();
        if (empty($where)) {
            $category = get_tree_option($category, 0);
        } else {
            foreach ($category as &$value) {
                $value['depth'] = false;
            }
        }
        $this->assign('data', $category);
        return $this->fetch();

        return $this->fetch();
    }

    // 删除动态分类
    public function delUserRole()
    {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = db('user_entertainment')->where(['parent_id'=>['<>', 0]])->delete($id);
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }

    // 添加动态分类
    public function addUserRole()
    {
        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');;

            //开始验证
            $validate = new Validate([
                ['name', 'require|max:20', '名称不能为空|长度不能超过20字符']
            ]);

            $data = [
                'name' => $post['name']
            ];

            if ($validate->check($data)) {
                if (!$id) {  //添加数据
                    $typename = db('user_entertainment')->where(['name' => $post['name']])->find();
                    if ($typename) {
                        return ['err' => '名称已存在'];
                    }
                    $time = time();
                    $post['create_time'] = $time;
                    $post['update_time'] = $time;


                    $res = db('user_entertainment')->insert($post);
                    if ($res) {
                        $data = ['ret' => '添加成功'];
                    } else {
                        $data = ['err' => '添加失败'];
                    }
                } else {//修改数据
                    $res = db('user_entertainment')->where('id', $id)->update($post);
                    if ($res) {
                        $data = ['ret' => '修改成功'];
                    } else {
                        $data = ['err' => '修改失败'];
                    }
                }
            } else {
                $data = ["err" => $validate->getError()];
            }
        } else {
            $tid = input('tid',0);
            if ($id) {
                $name = db('user_entertainment')->where('id', $id)->find();

                if ($name) {
                    if ($name['parent_id']) {
                        $tid = 1;
                    }
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }
            $this->assign('tid', $tid);

            $data = $this->fetch();
        }

        return $data;
    }


    public function reject()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.type');

        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $order = input('order') ? input('order') : 'asc';
        if ($order == 1) {
            $order = 'desc';
        } else if ($order == 2) {
            $order = 'asc';
        }

        $data = db('template_reject_reason')->where($where)->order('sort ' . $order . ',id ' . $order)->paginate(_getCachePages(), false, ['query' => request()->param()]);
        $page = $data->render();

        $this->assign('data', $data);
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function addReject()
    {
        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');
            $post['update_time'] = time();
            if (!$id) { //添加数据
                $post['create_time'] = $post['update_time'];
                $res = db('template_reject_reason')->insert($post);
                if ($res) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else { //修改数据

                $res = db('template_reject_reason')->where('id', $id)->update($post);

                if ($res) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = db('template_reject_reason')->where('id', $id)->find();

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }

    public function delReject()
    {
        if (request()->isAjax()) {
            $pid = input('id');
            if ($pid) {
                $res = db('template_reject_reason')->delete($pid);
                if ($res) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }
}
